Expand description

This crate provides a method to count allocations while running some code.

Example

Add as a dependency - since including the trait replaces the global memory allocator, you most likely want it gated behind a feature:

[features]
count-allocations = ["allocation-counter"]

[dependencies]
allocation-counter = { version = "0", optional = true }

Tests can now be written to assert that the number of desired memory allocations are not exceeded:

#[cfg(feature = "count-allocations")]
#[test]
pub fn no_memory_allocations() {
    let allocations = allocation_counter::count(|| {
        code_that_should_not_allocate_memory();
    });
    assert_eq!(allocations, 0);
}

Run the tests with the necessary feature enabled:

cargo test --features count-allocations

Functions

  • Run a closure while counting the performed memory allocations.